// components/checkboxgroup.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    checkstitle: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    checksarray: [{ key: 'key1', name: '选项1',checked:true }, { key: 'key2', name: '选项2'}],
    countries:[],
    countriesname:[],
    itemsize:0,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    set_data: function ({ checksarray, itemsize} = {}) {
      let data = {}
      if (checksarray !== undefined) {
        data['checksarray'] = checksarray
      }
      if (itemsize !== undefined) {
        data['itemsize'] = itemsize
      }
      this.setData(data)
    },
    event: function (event) {
      this.triggerEvent(event, this.data.countries, {})
    },
    onCheckboxChange: function (e) {
      var fieldName = e.currentTarget.dataset.name
      var thisdata = this.data
      var data = {}
      data[fieldName] = e.detail.value
      var names = []
      e.detail.value.forEach(function (item) {
        var obj = thisdata.checksarray.find(function (x) {
          return x.key == item
        })
        names.push(obj.name)
      })
      data[fieldName + 'name'] = names
      this.setData(data)
      this.event('change')
    }
  }
})
